extends Control
class_name MouseSlot
#这是鼠标的储存空间，基本就是没有BG的slot
# 打开UI时会跟随鼠标移动，具备保存物品信息的能力
# 当保存了物品信息的时候， 会显示物品对应的图标(和数量，如果有的话)
# 数量是独立于背包储存的数据

@onready var item_icon:TextureRect = $ItemIcon
@onready var update_timer:Timer = $UpdateTimer
@onready var name_label:Label =  $ItemIcon/NameLabel


@export var item_info_dict:Dictionary = {}
@export var item_icon_path_key:String = "icon"

## 上一个交换的slot
var last_slot: Slot
var item_pandora: PandoraEntity
var item_amount:int = 0

func _ready():
	# 初始化UI大小
	initTimer()

func _process(_delta):
	global_position = get_global_mouse_position() - Vector2(26, 26)

func initTimer():
	update_timer.wait_time = 0.01
	update_timer.timeout.connect(OnUpdateTimer_Timeout)
	update_timer.start()

	
func OnUpdateTimer_Timeout():
	UpdateItemInfo()

func clear_mouse_slot():
	item_info_dict.clear()
	name_label.text = ""
	item_icon.texture = null
	
func UpdateItemInfo():
	if item_info_dict :
		if item_info_dict.has("id"):
			name_label.text = str(item_info_dict["id"])
		if item_info_dict.has(item_icon_path_key):
			item_icon.texture = load(item_info_dict[item_icon_path_key])
	else:
		# 空格子的显示方式
		name_label.text = ""
		item_icon.texture = null

func _notification(what):
	if what == NOTIFICATION_DRAG_BEGIN:
		EventBus.push_event("to_sell_start", [item_info_dict["level"]])
	if what == NOTIFICATION_DRAG_END:
		EventBus.push_event("to_sell_end", [])
		if !get_viewport().gui_is_drag_successful():
			last_slot.swap_item_with_mouse_slot(true)
			last_slot = null
			return
		if not item_info_dict.is_empty() and last_slot != null:
			last_slot.swap_item_with_mouse_slot(true)
			last_slot = null
